package projectiteu;

import java.util.HashMap;

public class StandardIO {

    HashMap<String, String> convert = new HashMap<>();
    HashMap<String, String> vars = new HashMap<>();

    public int countNextLine(String inputLine) {
        int count = 0;
        char[] chrs = inputLine.toCharArray();
        for (int i = 0; i < chrs.length; i++) {
            if (chrs[i] == '\\' && inputLine.substring(i, i + 2).equals("\\n")) {
                count++;
            } else if (chrs[i] == 'e' && inputLine.substring(i, i + 4).equals("endl")) {
                count++;
            }
        }
        return count;
    }

    public String convertOutputToC(String inputLine) {
        int noOfNewLines = countNextLine(inputLine);
        String[] tmps = inputLine.split(" ");
        boolean isEndl = (tmps[tmps.length - 1].equals("endl;") || tmps[tmps.length - 1].equals("\"\\n\";")) ? true : false;
        if (tmps[tmps.length - 1].equals("endl;")) {
            inputLine = inputLine.substring(0, inputLine.length() - 9) + ";";
        } else if (tmps[tmps.length - 1].equals("\"\\n\";")) {
            inputLine = inputLine.substring(0, inputLine.length() - 9) + ";";
        }

        noOfNewLines = isEndl ? --noOfNewLines : noOfNewLines;

        inputLine = inputLine.replace("cout << ", (isEndl ? "Console.Writeline(" : "Console.Write("));
        inputLine = inputLine.replace(";", ");");
        inputLine = inputLine.replaceAll("<<", "+");
        inputLine = inputLine.replaceAll("endl", "\"\\\\n\"");

        return inputLine;
    }

    public String convertInputToC(String inputLine) {
        String[] tmps = inputLine.split(" ");
        initialize();

        String var = tmps[tmps.length - 1].substring(0, tmps[tmps.length - 1].length() - 1);
        System.out.println(var);
        if (vars.get(var).equals("string")) {
            inputLine = var + " = Console.ReadLine();";
        } else {
            inputLine = var + " = " + convert.get(vars.get(var)) + "Console.ReadLine());";
        }

        return inputLine;
    }

    private void initialize() {
        convert = new HashMap<>();
        convert.put("byte", "Convert.toByte(");
        convert.put("short", "Convert.toInt16(");
        convert.put("int", "Convert.toInt32(");
        convert.put("long", "Convert.toInt64(");
        convert.put("float", "Convert.toFloat(");
        convert.put("double", "Convert.toDouble(");
        convert.put("bool", "Convert.toBoolean(");
    }

    public void initialize(String input) {
        String[] tmps = input.split(" ");
        String dataType = tmps[0];
        for (int i = 1; i < tmps.length; i++) {
            String x = tmps[i];
            if (x.contains(";")) {
                x = x.substring(0, x.length() - 1);
                vars.put(x, dataType);
            } else if (x.equals("=")) {
                vars.put(tmps[i - 1], dataType);
            }
        }
        System.out.println("Vars: " + vars);
    }

    private boolean isDataType(String tmp) {
        if (tmp.equals("int") || tmp.equals("float") || tmp.equals("byte") || tmp.equals("double") || tmp.equals("long") || tmp.equals("boolean")
                || tmp.equals("char") || tmp.equals("string")) {
            return true;
        } else {
            return false;
        }
    }
}
